using Microsoft.EntityFrameworkCore;
using StudentManagement.Domain.Entities;
using StudentManagement.Domain.Repositories;
using StudentManagement.Infrastructure.Data;

namespace StudentManagement.Infrastructure.Repositories;

public class ClassRepository : Repository<Class>, IClassRepository
{
    public ClassRepository(ApplicationDbContext context) : base(context)
    {
    }

    public async Task<IEnumerable<Class>> GetClassesByGradeAsync(string grade)
    {
        return await _dbSet.Where(c => c.Grade == grade).ToListAsync();
    }

    public async Task<IEnumerable<Class>> GetClassesByMajorAsync(string major)
    {
        return await _dbSet.Where(c => c.Major == major).ToListAsync();
    }

    public async Task<IEnumerable<Class>> SearchClassesAsync(string keyword)
    {
        if (string.IsNullOrWhiteSpace(keyword))
        {
            return await GetAllAsync();
        }

        keyword = keyword.ToLower();
        return await _dbSet
            .Where(c => c.ClassName.ToLower().Contains(keyword) ||
                        c.Grade.ToLower().Contains(keyword) ||
                        c.Major.ToLower().Contains(keyword) ||
                        c.Department.ToLower().Contains(keyword) ||
                        c.HeadTeacher.ToLower().Contains(keyword))
            .ToListAsync();
    }

    public async Task<IEnumerable<Class>> GetClassesWithStudentsAsync()
    {
        return await _dbSet.Include(c => c.Students).ToListAsync();
    }
}